package com.example.agrigreennewssys.service.impl;


import com.example.agrigreennewssys.dao.CommentLikeMapper;
import com.example.agrigreennewssys.dao.CommentMapper;
import com.example.agrigreennewssys.entity.ArticleLike;
import com.example.agrigreennewssys.entity.Comment;
import com.example.agrigreennewssys.entity.CommentLike;
import com.example.agrigreennewssys.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 光的代言人
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentLikeMapper commentLikeMapper;

//    @Autowired
//    private CommentLikeMapper.xml commentLikeMapper;

    @Override
    @Transactional
    public Comment addComment(Comment comment) {
        comment.setStatus(1);
        comment.setLikes(0);
        comment.setCreatedAt(String.valueOf(new java.util.Date()));
        comment.setUpdatedAt(String.valueOf(new java.util.Date()));

        commentMapper.insert(comment);
        return commentMapper.selectById(comment.getId());
    }

    @Override
    @Transactional
    public int deleteComment(Long commentId, Long userId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null || !comment.getUserId().equals(userId)) {
            throw new RuntimeException("无权删除该评论");
        }
        return commentMapper.deleteById(commentId);
    }

    /**
     * 获取文章下的所有评论
     *
     * @param articleId 文章ID
     * @return 评论列表
     */
    @Override
    public List<Comment> getCommentsByArticleId(Long articleId, Long currentUserId) {
        List<Comment> comments = commentMapper.selectByArticleId(articleId);
//        if (currentUserId != null) {
//            // 设置是否已被当前用户点赞
//            comments.forEach(comment -> {
//                Integer exists = commentLikeMapper.exists(comment.getId(), currentUserId);
//                comment.setLikedByCurrentUser(exists != null && exists > 0);
//            });
//        }
        return comments;
    }

    @Override
    public List<Comment> getAllComments() {
        return commentMapper.selectAllComments();
    }

    @Override
    public Comment getCommentById(Long commentId) {
        return commentMapper.selectById(commentId);
    }

    @Override
    public boolean updateCommentStatus(Long commentId, Integer status) {
        return commentMapper.updateStatus(commentId, status) > 0;
    }

    @Override
    public boolean isCommentLiked(Long commentId, Long userId) {
        System.out.println("commentId"+commentId+",userId:"+userId);
        Integer status = commentLikeMapper.getCommentLikeStatus(commentId, userId);
        return status != null && status == 1;
    }

    @Transactional
    @Override
    public boolean likeComment(Long commentId, Long userId,Long articleId) {
        Integer status = commentLikeMapper.getCommentLikeStatus(commentId, userId);
        if (status != null && status == 1) {
            // 该评论已经点赞过了
            return false;
        }
        if (status == null) {
            // 第一次点赞
            CommentLike commentLike = new CommentLike();
            commentLike.setCommentId(commentId);
            commentLike.setArticleId(articleId);
            commentLike.setUserId(userId);
            commentLike.setStatus(1);
            commentLike.setCreatedAt(new Date());
            commentLikeMapper.insertCommentLike(commentLike);
        } else {
            commentLikeMapper.updateCommentLikeStatus(commentId, userId, 1);
        }
//        articleMapper.incrementArticleLikes(articleId);
        return true;
    }

    @Override
    public boolean unlikeComment(Long commentId, Long userId) {
        Integer status = commentLikeMapper.getCommentLikeStatus(commentId, userId);
        // 该评论已经取消点赞过了
        if (status == null || status == 0) {
            return false;
        }
        commentLikeMapper.updateCommentLikeStatus(commentId, userId, 0);
//        articleMapper.decrementArticleLikes(articleId);
        return true;
    }

    @Override
    public List<CommentLike> selectCommentLikeList(String nickName, String title, String content) {
        return commentLikeMapper.selectCommentLikeList(nickName,title,content);
    }
    /**
     * 点赞评论
     *
     * @param commentId 评论ID
     * @param userId    用户ID
     * @return 是否成功
     */
//    @Override
//    @Transactional
//    public boolean likeComment(Long commentId, Long userId) {
//        // 检查是否已点赞
//        if (commentLikeMapper.exists(commentId, userId) > 0) {
//            return false;
//        }
//
//        // 记录点赞
//        CommentLike like = new CommentLike();
//        like.setCommentId(commentId);
//        like.setUserId(userId);
//        commentLikeMapper.insert(like);
//
//        // 更新评论点赞数
//        commentMapper.incrementLikes(commentId);
//        return true;
//    }
    /**
     * 取消点赞评论
     *
     * @param commentId 评论ID
     * @param userId    用户ID
     * @return 是否成功
     */
//    @Override
//    @Transactional
//    public boolean unlikeComment(Long commentId, Long userId) {
//        // 检查是否已点赞
//        if (commentLikeMapper.exists(commentId, userId) == 0) {
//            return false;
//        }
//
//        // 删除点赞记录
//        commentLikeMapper.deleteByCommentIdAndUserId(commentId, userId);
//
//        // 更新评论点赞数
//        commentMapper.decrementLikes(commentId);
//        return true;
//    }
}
